<?php

	class Member extends Member_model {

		function __construct() {
			parent::__construct();
		}
		
		function validateChangePassword($new_password, $confirm_password) {
			if (trim($new_password) == "")
			{
				MessageHandler::add (_LB_INVALID_PASSWORD, MSG_ERROR, LOG_ONLY);
			}
			elseif(strlen($new_password) > MAX_LENGTH_PASSWORD)
			{
				MessageHandler::add (_MSG_PASSWORD_IS_TOO_LONG, MSG_ERROR, LOG_ONLY);
			}
			elseif(strlen($new_password) < MIN_LENGTH_PASSWORD)
			{
				MessageHandler::add (_MSG_PASSWORD_IS_TOO_SHORT, MSG_ERROR, LOG_ONLY);
			}
			elseif (trim($confirm_password) == "") {
				MessageHandler::add (_LB_INVALID_CONFIRM_PASSWORD, MSG_ERROR, LOG_ONLY);
			} elseif ($new_password != $confirm_password)
			{
				MessageHandler::add (_LB_PASSWORD_DOESNT_MATCH, MSG_ERROR, LOG_ONLY);
			}
			return MessageHandler::countError() > 0 ? false : true;
		}
		
		function userExistByEmail($email, $id) {

			$t = new Member();
			$t->addWhere("email = '$email'");
			
			if ($id)
				$t->addWhere("id <> $id");

			$t->find();
			if($t->fetchFirst()) {
				return $t->id;
			}
			return 0;
		}
		
		function userExistByUsername($username, $id) {

			$t = new Member();
			$t->addWhere("username = '$username'");
			
			if ($id)
				$t->addWhere("id <> $id");
			
			$t->find();
			if($t->fetchFirst()) {
				return $t->id;
			}
			return 0;
		}
		
		function userExistByIdentify($identify, $id) {

			$t = new Member();
			$t->addWhere("indentify = '$identify'");
			
			if ($id)
				$t->addWhere("id <> $id");
			
			$t->find();
			if($t->fetchFirst()) {
				return $t->id;
			}
			return 0;
		}
		
		function store() {
			if ($this->id==0)
				return $this->insert();
			else return $this->update();
		}
		
		function getList($filter = array()) {
			$user = new Member();
			if(isset($filter['name']) && $filter['name']) {
				$user->addWhere("member.fullname LIKE '%".$filter['name']."%'");
			}
			if(isset($filter['username']) && $filter['username']) {
				$user->addWhere("member.username LIKE '%".$filter['username']."%'");
			}
			if(isset($filter['indentify']) && $filter['indentify']) {
				$user->addWhere("member.indentify LIKE '%".$filter['indentify']."%'");
			}
			if(isset($filter['email']) && $filter['email']) {
				$user->addWhere("member.email LIKE '%".$filter['email']."%'");
			}
			$user->addSelect();
			$user->addSelect("member.*");
			$user->orderBy("member.id DESC");
			$user->find();
			
			return $user;            
		}
		
		function validateInput($confirm_password = null) {
			
			if (trim($this->username) == "") {
				MessageHandler::add (_MSG_USERNAME_IS_EMPTY, MSG_ERROR, LOG_ONLY);
			}
			else if (strlen($this->username) > MAX_LENGTH_USERNAME) {
				MessageHandler::add (_MSG_USERNAME_IS_TOO_LONG, MSG_ERROR, LOG_ONLY);
			}
			else if (Member::userExistByUsername($this->username, $this->id)) {
				MessageHandler::add (_MSG_USERNAME_EXISTS, MSG_ERROR, LOG_ONLY);
			}
			else if (trim($this->indentify) == "") {
				MessageHandler::add (_MSG_CARD_PASSPORT_IS_EMPTY, MSG_ERROR, LOG_ONLY);
			}
			else if (strlen($this->indentify) > 12) {
				MessageHandler::add (_MSG_CARD_PASSPORT_IS_TOO_LONG, MSG_ERROR, LOG_ONLY);
			}
			else if (Member::userExistByIdentify($this->indentify, $this->id)) {
				MessageHandler::add (_MSG_CARD_PASSPORT_EXISTS, MSG_ERROR, LOG_ONLY);
			}
			else if (trim($this->fullname) == "") {
				MessageHandler::add (_MSG_FULL_NAME_IS_EMPTY, MSG_ERROR, LOG_ONLY);
			}
			else if (strlen($this->fullname) > MAX_LENGTH_NAME) {
				MessageHandler::add (_MSG_FULL_NAME_IS_TOO_LONG, MSG_ERROR, LOG_ONLY);
			}
			else if (trim($this->birthday) == "") {
				MessageHandler::add (_MSG_BIRTHDAY_IS_EMPTY, MSG_ERROR, LOG_ONLY);
			}
			else if (!isset($this->email) || trim($this->email) == "") {
				MessageHandler::add (_MSG_EMAIL_IS_EMPTY, MSG_ERROR, LOG_ONLY);
			}
			elseif (!isValidEmail($this->email))
			{
				MessageHandler::add (_MSG_INVALID_EMAIL, MSG_ERROR, LOG_ONLY);
			}
			else if (strlen($this->email) > MAX_LENGTH_NAME)
			{
				MessageHandler::add (_MSG_EMAIL_IS_TOO_LONG, MSG_ERROR, LOG_ONLY);
			}
			else if (Member::userExistByEmail($this->email, $this->id))
			{
				MessageHandler::add (_MSG_EMAIL_EXISTS, MSG_ERROR, LOG_ONLY);
			}
			else if (trim($this->address) == "") {
				MessageHandler::add (_MSG_ADDRESS_IS_EMPTY, MSG_ERROR, LOG_ONLY);
			}
			// When add new
			if (!$this->id)
			{
				$this->validateChangePassword($this->password, $confirm_password);
			}
			
			return MessageHandler::countError() > 0 ? false : true;
		}
	}
